package com.yc.bran.snack.service.impl;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yc.bran.snack.bean.MemberInfo;
import com.yc.bran.snack.mapper.IMemberInfoMapper;
import com.yc.bran.snack.service.IMemberInfoService;
import com.yc.bran.snack.util.StringUtil;

@Service
public class MemberInfoServiceImpl implements IMemberInfoService {
	
	@Autowired
	private IMemberInfoMapper memberInfoMapper;
	
	private static final String path = "D://snackpics//";
	
	@Override
	public MemberInfo login(MemberInfo mf) {
		if(StringUtil.checkNull(mf.getUname(), mf.getUpwd())) {
			return null;
		}
		return memberInfoMapper.login(mf);
	}

	@Override
	public int reg(MemberInfo mf) {
		if(StringUtil.checkNull(mf.getUname(), mf.getUtel(), mf.getUpwd())) {
			return -100;
		}
		return memberInfoMapper.reg(mf);
	}

	@Override
	public List<MemberInfo> list() {
		return memberInfoMapper.list();
	}

	@Override
	public int closeuser(Integer uid) {
		return memberInfoMapper.closeuser(uid);
	}

	@Override
	public int openuser(Integer uid) {
		return memberInfoMapper.openuser(uid);
	}
	
	// 保存到服务器  
    @Override  
    public String saveProfilePic(MultipartFile file, Integer uid) {  
        if (file.isEmpty()) {  
            throw new IllegalArgumentException("上传的文件为空！");  
        } 
        
        File dir = new File(path);  
        if (!dir.exists()) {  
            dir.mkdirs();  
        }
        
        String upic =String.valueOf(uid) + "_" + file.getOriginalFilename();
  
        Path filePath = Paths.get(path + upic);
        
        try {  
            Files.write(filePath, file.getBytes());  
            return upic.toString();
        } catch (IOException e) {  
            throw new RuntimeException("保存文件时发生错误！", e);  
        }  
    }  
  
    // 更新数据库中的头像路径  
    @Override  
    public void updateProfilePic(Integer uid, String upic) {  
    	memberInfoMapper.updateProfilePic(uid, upic);
    }  
}
